package com.yy.wga.user.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.wga.common.util.aop.DistributedLock;
import com.yy.wga.user.service.mapper.UserSummaryMapper;
import com.yy.wga.user.service.pojo.UserSummaryDO;
import com.yy.wga.user.service.service.UserSummaryService;
import org.springframework.stereotype.Service;

/**
 * 用户数据统计(UserSummary)表服务实现类
 *
 * @author guochao
 * @since 2024-12-22 11:07:27
 */
@Service
public class UserSummaryServiceImpl extends ServiceImpl<UserSummaryMapper, UserSummaryDO> implements UserSummaryService {
    @Override
    @DistributedLock(value = "#updateInfo.userId")
    public void updateInfo(UserSummaryDO updateInfo) {
        UserSummaryDO userSummary = baseMapper.selectOne(new LambdaQueryWrapper<UserSummaryDO>()
                .eq(UserSummaryDO::getAppKey, updateInfo.getAppKey())
                .eq(UserSummaryDO::getUserId, updateInfo.getUserId()));
        Integer saleAmount = 0;
        if (userSummary != null) {
            saleAmount = userSummary.getSaleAmount();
            updateInfo.setId(userSummary.getId());
        }
        // 销售额托底为0
        Integer amount = saleAmount + updateInfo.getSaleAmount();
        if (amount < 0) {
            amount = 0;
        }
        updateInfo.setSaleAmount(amount);
        this.saveOrUpdate(updateInfo);
    }

    @Override
    @DistributedLock(value = "#updateInfo.userId")
    public void downInfo(UserSummaryDO updateInfo) {
        UserSummaryDO userSummary = baseMapper.selectOne(new LambdaQueryWrapper<UserSummaryDO>()
                .eq(UserSummaryDO::getAppKey, updateInfo.getAppKey())
                .eq(UserSummaryDO::getUserId, updateInfo.getUserId()));

        if (userSummary != null) {
            updateInfo.setId(userSummary.getId());
            updateInfo.setDeleted(userSummary.getDeleted());
        }
        updateInfo.setSaleAmount(updateInfo.getSaleAmount());
        this.saveOrUpdate(updateInfo);
    }
}
